import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router';
import BasicLayout from '../layouts/BasicLayout.vue';
import BlankLayout from '../layouts/BlankLayout.vue';
import WelcomePage from '../views/Page1.vue';


// only githubpages preview site used, if use template please remove this check
// and use `createWebHistory` is recommend
const hasGithubPages = import.meta.env.VITE_GHPAGES;

const router = createRouter({
  history: hasGithubPages ? createWebHashHistory() : createWebHistory(),
  routes: [
    {
      path: '/login',
      name: 'login',
      meta: { title: 'Login' },
      component: ()=>import ('@/views/user/Login.vue'),
    },
    {
      path: '/register',
      name: 'register',
      meta: { title: 'Register' },
      component: ()=>import ('@/views/user/Register.vue'),
    },
    {
      path: '/detail',
      name: 'detail',
      meta: { title: '用户详情' },
      component: () => import('@/components/UserManagement/Detail.vue'),
    },
    {
      path: '/',
      name: 'index',
      meta: { title: 'Home',requiresAuth:true },
      component: BasicLayout,
      redirect: '/welcome',
      children: [
        {
          path: '/welcome',
          name: 'welcome',
          meta: { title: '欢迎', icon: 'icon-icon-test' },
          component: WelcomePage,
        },
        {
          path: '/admins',
          name: 'admins',
          meta: { title: '管理页', icon: 'icon-tuijian', flat: true },
          component: BlankLayout,
          redirect: () => ({ name: 'page1' }),
          children: [
            {
              path: 'page-1',
              name: 'page1',
              meta: { title: '管理员' },
              component: () => import('../views/admins/PageInfo.vue'),
              // beforeEnter:()=>{
              //   console.log('禁止跳转')
              //   return false;
              // }
            },
            {
              path: 'page-2',
              name: 'page2',
              meta: { title: '用户管理' },
              component: () => import('@/components/UserManagement/UserManagement.vue'),
            },
            // {
            //   path: 'Test',
            //   name: 'Test',
            //   meta: { title: 'Test' },
            //   component: () => import('@/views/Test.vue'),
            // },
            {
              path: 'dynamic-match/:id(\\d+)',
              name: 'dynamic-match',
              // 路由 path 默认参数再 meta.params 里
              meta: { title: '用户情况', params: { id: 1 } },
              component: () => import('../views/admins/DynamicMatch.vue'),
            },
          ],
        },
        {
          path: '/version',
          name: 'version',
          meta: { title: 'Version', icon: 'icon-antdesign' },
          component: () => import('../views/Detail.vue'),
        },
      ],
    },
    
  ],
});


router.beforeEach(async (to, from, next) => {
  console.log(to);
  console.log(from);
  console.log(next);
  
  // 获取 token;await getUserInfo(userAccount)===null
  if (to.matched.some(record => record.meta.requiresAuth)) {
    const userAccount=localStorage.getItem('userAccount')
    if (userAccount===null||userAccount==='null') {
      next({
        path: '/login',
        query: { redirect: to.fullPath },
      });
    } else {
      console.log(userAccount)
      next();
    }
  } else {
    next();
  }
});

export default router;